﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BPM.Service;
using BPM.Entity;
using BPM;

namespace WebTest.Services
{
	[TestClass]
	public class MaterialDefinitionSvcTest : ServiceTestBase<MaterialDefinitionService, MaterialDefinition>
	{
		[ClassInitialize()]
		public static void ClassInitialize(TestContext testContext)
		{
			InitializeApplication();
		}

		[TestInitialize()]
		public override void TestInitialize()
		{
			base.TestInitialize();
		}

		[TestCleanup()]
		public override void TestCleanup()
		{
			base.TestCleanup();
		}

		[TestMethod]
		public void MaterialDefinitionSvcTest_Add_Delete()
		{
			var oldCateCount = new MaterialDefinitionCategoryService().Count();
			var oldUserCount = new UserService().Count();
			var oldBarcodeCount = new BarcodeService().Count();

			var cateSvc = new MaterialDefinitionCategoryService();
			var cate = cateSvc.GetBigCates().First().SubCategoryList.First();

			var entity = new MaterialDefinition()
			{
				Brand = "Brand",
				CategoryId = cate.Id,
				CreatorId = SysUsers.Administrator.Id,
				ContactName = "联系人名字",
				ContactPhone = "联系电话",
				DeliveryPeriod = 30,
				DeliverPeriodUnit = "日",
				ImagePath = "~/upload/images/Material/test.jpg",
				Name = "单元测试产品",
				OtherContact = "没有其它联系人",
				PaymentMode = "没有付款方式",
				Price = 100,
				SupplierName = "没有供应商名字",
				Remark = "没有备注"
			};
			var res = this._svc.Save(entity);
			Assert.IsTrue(res, res);
			Assert.IsNotNull(entity.Barcode);
			Assert.AreEqual(oldCateCount, new MaterialDefinitionCategoryService().Count());
			Assert.AreEqual(oldUserCount, new UserService().Count());
			Assert.AreEqual(oldBarcodeCount + 1, new BarcodeService().Count());

			entity.Name = "单元测试产品-修改后";
			entity.IsApproved = true;
			res = this._svc.Save(entity);
			Assert.IsTrue(res, res);
			entity = this._svc.GetById(entity.Id);
			Assert.AreEqual(entity.Name, "单元测试产品-修改后");

			res = this._svc.Delete(entity.Id);
			Assert.IsTrue(res, res);

			Assert.AreEqual(oldCateCount, new MaterialDefinitionCategoryService().Count());
			Assert.AreEqual(oldUserCount, new UserService().Count());
			Assert.AreEqual(oldBarcodeCount, new BarcodeService().Count());
		}

		[TestMethod]
		public void MaterialDefinitionSvcTest_Import_Delete()
		{
		}

		[TestMethod]
		public void MaterialDefinitionSvcTest_Add100()
		{
			return;
			for (int i = 0; i < 100; i++)
			{
				var cateSvc = new MaterialDefinitionCategoryService();
				var cate = cateSvc.GetBigCates().First().SubCategoryList.First();

				var entity = new MaterialDefinition()
				{
					Brand = "Brand",
					CategoryId = cate.Id,
					CreatorId = SysUsers.Administrator.Id,
					ContactName = "联系人名字",
					ContactPhone = "联系电话",
					DeliveryPeriod = 30,
					DeliverPeriodUnit = "日",
					ImagePath = "~/upload/images/Material/test.jpg",
					Name = "单元测试产品" + i,
					OtherContact = "没有其它联系人",
					PaymentMode = "没有付款方式",
					Price = 100,
					SupplierName = "没有供应商名字",
					Remark = "没有备注"
				};
				var res = this._svc.Save(entity);

				entity.IsApproved = true;
				this._svc.Save(entity);
			}
		}
	}
}
